= {'jack': 4098, 'sape': 4139} tel
03-13-24 (Wednesday)
VERSE 1:
We bring our time, we bring our treasure,
we lay them down before Your throne.
You will make them something greater,
more than we could ever know. (chorus)
VERSE 2:
We bring our gifts, we bring our power
place them in Your sov’reign hand.
You will take what we have given,
You will use it for Your plan. (chorus)
CHORUS:
Glory be to God, the Maker
glory be to God, Creator
Take our time, use our treasure
turn them into something greater:
Glory be to God, the Maker.
VERSE 3:
Though our hearts are weak from failure,
broken dreams and failed attempts,
show us that in ev’ry season,
You will fill our emptiness. (chorus)
(God the Maker, The Porter’s Gate)
1 Techniques and patterns with dictionaries
1.1 Methods
From Python Documentation:
- Take a look at methods you may find useful and note them down. here. Try to “play around” with them later.
1.2 Constructors
- There are many ways to construct a dictionary.
= dict([('sape', 4139), ('guido', 4127), ('jack', 4098)]) tel
= dict(sape=4139, guido=4127, jack=4098) tel
- You can even use dictionary comprehensions. What will be the resulting dictionary in the following program?
= {name : {x: 0 for x in range(1990,2023)} for name in ['Ronaldo', 'Rivaldo', 'Neymar']} goals
1.3 Nesting dictionaries example: XML files
- The following code uses the
requests
module to download a XML file, and the ElementTree class to parse it into nested dictionaries. - The XML file was generated by the BoardGameGeek website and contains your professor’s board game collection. :D
import requests
import xml.etree.ElementTree as ET
# URL of the XML file
= "https://cs.calvin.edu/courses/cs/108/fsantos/units/08/activities/bgg_collection.xml"
url
# Send a GET request to the URL to retrieve the XML content
= requests.get(url)
response
# Check if the request was successful
if response.status_code == 200:
# Parse the XML content
= response.content
xml_content = ET.fromstring(xml_content)
root
= {}
collection # Process the XML data
for item in root:
= item.find('name').text # get the game name
gamename
= {}.copy() # construct the dictionary with the game's attributes
game = game
collection[gamename]
'year'] = item.find('yearpublished').text
game['stats'] = item.find('stats').attrib game[
- Note: XML is a very common data interchange format. Its main advantage is being application and language independent. You can check some basics here.
1.4 Looping through dictionaries
- Try to run the code in your machine and now write some code to loop through all the names and stats of the games. Make it a function!
- For example:
for game, value in collection.items():
print(game+': ')
for att, v in value.items():
print(att, v)
print()
- Try to remove some games from this dictionary collection using the
del
statement. You can even use a loop:
for game in ['Abstratus', 'Agricola', 'Whale Riders']:
del collection[game]
2 Sets
- A set is an unordered collection with no duplicate elements.
- Curly braces or the
set()
function can be used to create sets. - Important: to create an empty set you have to use
set()
, not{}
; the latter creates an empty dictionary!
2.1 Use example: find unique elements
= "banana"
a = set(a)
unique print(unique) # all the letters used in the string 'banana'
{'a', 'b', 'n'}
- Later, for example, you may want to use this set as the keys for a dictionary counting the number of occurrences.
= {u:0 for u in unique}
occurrences for character in a:
+= 1
occurrences[character] print(occurrences)
{'a': 3, 'b': 1, 'n': 2}
2.2 Use example: set operations
- Another application of sets is to perform mathematical operations such as union, intersection, difference, and symmetric difference. These operations can be useful in various scenarios, such as data analysis, where you need to combine or compare sets of data.
- Suppose my wife and I had two book collections before getting married. Then…
= {"The Lord of the Rings", "Out of the Silent Planet", "The Chronicles of Narnia", "The Silmarillion"}
fernando = {"Harry Potter", "Pride and Prejudice", "The Lord of the Rings", "The Chronicles of Narnia", "The Hobbit"}
jemima
= fernando.union(jemima)
all_books print("All Books:", all_books)
All Books: {'The Silmarillion', 'Harry Potter', 'Out of the Silent Planet', 'The Hobbit', 'The Chronicles of Narnia', 'The Lord of the Rings', 'Pride and Prejudice'}
- But let’s suppose we also want to compare our collections:
# Union of the two sets (books that we both have)
= fernando.intersection(jemima)
common_books print("Common books:", common_books)
# Difference between the two sets (books that Fernando has and Jemima doesn't)
= fernando.difference(jemima) # OR: fernando - jemima
unique_to_fernando print("Books unique to Fernando:", unique_to_fernando)
# Symmetric difference (books that either Fernando has or Jemima has)
= fernando.symmetric_difference(jemima)
unique_books print("Unique books in both collections:", unique_books)
Common books: {'The Lord of the Rings', 'The Chronicles of Narnia'}
Books unique to Fernando: {'The Silmarillion', 'Out of the Silent Planet'}
Unique books in both collections: {'The Silmarillion', 'Pride and Prejudice', 'Out of the Silent Planet', 'Harry Potter', 'The Hobbit'}
2.3 Set methods
All the methods you can use with sets can be found in the documentation.
You can also use set comprehensions. For example, what’s the output of the following code?
= {x for x in 'abracadabra' if x not in 'abc'} a
3 Free, libre and open source software
- The source code of proprietary software may typically not be modified or redistributed without express consent.
- However, Free, Libre and Open Source Software (FLOSS) is software whose source code may be modified and redistributed. Anyone is allowed to view, modify, and contribute to the development.
- “Libre” is a term often used in the open-source community to emphasize the freedom to use, study, modify, and distribute software. This is in contrast to “gratis,” which simply means “free of charge.”
It can be useful to distinguish between four levels of free software:
FREEDOM 0: freedom to run the program as you wish, for any purpose.
FREEDOM 1: freedom to study how the program works, and change it so it does your computing as you wish. Access to the source code is a precondition for this.
FREEDOM 2: freedom to redistribute copies so you can help others.
FREEDOM 3: freedom to distribute copies of your modified versions to others. By doing this you can give the whole community a chance to benefit from your changes.
There are many kinds and standards of open software licenses.
- GNU General Public License (GPL): One of the most widely used open-source licenses, the GPL requires that any derivative work also be licensed under the GPL. It originated with Richard Stallman, one of the father os FLOSS, and it forbids any modification of the code to be directly used in a proprietary product. Advocates of this stance often reject the term “open source,” which dates to early 1998, and use Stallman’s original “free software,” because copyleft licenses are to protect the freedom of the developer and all future users to do whatever they want with the software.
- MIT License: A very permissive license that allows for almost unrestricted use of the software, including commercial use, as long as the original copyright notice and disclaimer are included in all copies or substantial portions of the software.
- Apache License,
- BSD License,
- Mozilla Public License,
- Creative Commons License, etc.
3.2 The economic aspect: FLOSS enables stewardship
What is good: - It is free! And then it can help the underpriviledged by giving access to something. - Thus, it can be an attitude of self-giving and stewardship. - Some people notice that “software is an “antirival” good. Not only does the value of software not diminish if more people use it, including freeloaders (as opposed to the “tragedy of the commons”), but its value may also increase with additional users—for example, when they contribute bug reports or other suggestions.”
Problems and challenges: - It can precarize and invisibilize work that should be rewarded (Ivan Illich’s “shadow work”). - It may become a drug, creating dependency in other areas.
3.3 The juridical aspect: FLOSS enables freedom
What is good: - It can subvert authorities and offer different approaches to computer software solutions.
Problems and challenges: - It is often put under a libertarian ideal of free thought and free speech that could culminate in social anarchy.
3.4 The aesthetic aspect: FLOSS enables play and creativity
What is good: - Freedom from economic restraints permits an aesthetic and playful approach to developing something. We are not doing just for money or recognition, but for the sake of doing something nice (“internal good”, in contrast to an “external good”).
Problems and challenges: - At the same time, it can become irrelevant, too extravagant and not really attentive to what is most needed.
- What are other points you think might be important to be called attention?
- How can we, as Christians, help and propose better dynamics for software development? How can we affirm, critique and enrich this discussion?